package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api("员工管理")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工
     * @param employeeDTO 新增员工数据
     * @return 新增结果
     */
    @PostMapping("")
    @ApiOperation("新增员工")
    public Result<String> insEmployee(@RequestBody EmployeeDTO employeeDTO) {
        log.info("新增员工{}",employeeDTO);
        return employeeService.insEmployee(employeeDTO);
    }

    /**
     * 分页查询 员工信息
     * @param employeePageQueryDTO 查询参数
     * @return 查询结果
     */
    @GetMapping("/page")
    @ApiOperation("分页查询-员工信息")
    public Result<PageResult> getEmployeePageList(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("分页查询-员工信息:{}",employeePageQueryDTO);
        return employeeService.getEmployeePageList(employeePageQueryDTO);
    }

    /**
     * 禁用员工
     * @param status 禁用状态
     * @param id 员工ID
     * @return 返回操作结果
     */
    @PostMapping("/status/{status}")
    @ApiOperation("禁用员工")
    public Result<?> disableEmployeesStatus(@PathVariable("status") String status,Long id) {
        log.info("禁用员工:id{}",id);
        return employeeService.disableEmployeesStatus(status, id);
    }

    /**
     * 根据id查询员工信息
     * @param id 员工id
     * @return 员工信息
     */
    @GetMapping("/{id}")
    @ApiOperation("查询员工")
    public Result<?> getEmployeeById(@PathVariable("id") Long id) {
        log.info("查询员工信息:id{}",id);
        return employeeService.getEmployeeById( id);
    }

    /**
     * 编辑修改员工信息
     * @param employeeDTO 员工信息
     * @return 操作结果
     */
    @PutMapping("")
    @ApiOperation("编辑员工信息")
    public Result<?> updateEmployee(@RequestBody EmployeeDTO employeeDTO) {
        log.info("编辑员工信息:{}",employeeDTO);
        return employeeService.updateEmployee( employeeDTO);
    }
}
